package automakelist;

import config.Config;
import model.CClass;
import model.Coach;
import model.curriculum;
import util.WeekOfDay;

import java.util.*;

/**
 * Created by root on 17-6-21.
 */
public class computing {
    public static int coachweek_max_student(Config c) {
        int coach_maxhour = c.coach_work_day_hours * c.getworkingweekdaycount();
        return (int) Math.ceil((double) coach_maxhour / (double) c.week_h_max);
    }

    public static List<Integer> addwithnotexist(curriculum result, Config c, Coach coach, CClass cClass) {
        List<Integer> src = new ArrayList<>();
        while (true) {
            if (src.size() < c.studentcount_oncar) {
                int next_i_student = coach.findDay_h_notenough(result, src, c, cClass);
                if (next_i_student != -1) {
                    src.add(next_i_student);
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        return src;
    }

    public static curriculum spiltstudenttocoach_平均分配(curriculum cur, Config c) {
        curriculum result = cur;
        //平均分配学员给教练
        for (int i_student = 0, i_coach = 0; i_student < c.student_count; ) {
            Integer s = new Integer(i_student);
            result.coaches.get(i_coach).studentMap.put(i_student, s);
            result.coaches.get(i_coach).studentlist.add(s);
            i_student++;
            if (i_coach < c.coach_count - 1) {
                i_coach++;
            } else {
                i_coach = 0;
            }
        }

        return result;
    }

    public static curriculum spiltstudenttocoach_教练排满优先_预约考试时长(curriculum cur, Config c) {
        curriculum result = cur;
        int coach_maxstu = computing.coachweek_max_student(c);
        int per_coach_students = c.student_count / c.coach_count;
        if (coach_maxstu >= per_coach_students) {
            //学生少，教练多
            //优先把一个教练的一周时间排满
            for (int i_student = 0, i_coach = 0; i_student < c.student_count; ) {
                for (int i = 0; i < coach_maxstu && i_student < c.student_count; i++) {
                    Integer s = new Integer(i_student);
                    result.coaches.get(i_coach).studentMap.put(i_student, s);
                    result.coaches.get(i_coach).studentlist.add(s);
                    i_student++;
                }
                i_coach++;
            }
        } else {
            //学生多，教练少
            //优先把一个教练的一周时间排满
            for (int i_student = 0, i_coach = 0; i_student < c.student_count; ) {
                Integer s = new Integer(i_student);
                result.coaches.get(i_coach).studentMap.put(i_student, s);
                result.coaches.get(i_coach).studentlist.add(s);
                i_student++;
                if (i_coach < c.coach_count - 1) {
                    i_coach++;
                } else {
                    i_coach = 0;
                }
            }
        }
        return result;
    }

    public static curriculum 分配教练时间给他的学员(curriculum result, Date startdate, Config c) {
        //再部署教练的每节课的学员
        for (int i_coach = 0; i_coach < c.coach_count; i_coach++) {
            int day = 0;
            int class_index = 0;
            int class_count = c.classtimes.length;
            int week_times = 0;
            int week_class_count = 0;

            for (; ; ) {
                Calendar calendar = new GregorianCalendar();
                calendar.setTime(startdate);
                calendar.add(calendar.DATE, day);

                int weekday = WeekOfDay.dayForWeek(calendar.getTime());
                boolean isworkingday = c.workingday[weekday];
                if (isworkingday) {
                    //工作日
                    //region CClass
                    CClass cl = new CClass(c.classtimes[class_index]);
                    List<Integer> student_indexs = addwithnotexist(result, c, result.coaches.get(i_coach), cl);
                    if (student_indexs.size() > 0) {
                        for (int i_student = 0; i_student < student_indexs.size(); i_student++) {
                            int stuid = result.coaches.get(i_coach).studentMap.get(student_indexs.get(i_student));
                            result.students.get(stuid).all_hours += cl.getHours();
                            result.students.get(stuid).week_classhours += cl.getHours();
                            result.students.get(stuid).day_classhours += cl.getHours();
                        }
                        cl.studentsid = student_indexs;
                        cl.date = calendar.getTime();
                        cl.teacherid = i_coach;
                        result.coaches.get(i_coach).cClasses.add(cl);
                        week_class_count++;
                    }
                    class_index++;
                    //endregion
                    //一天课全上完
                    if (class_index >= class_count) {
                        for (int i_st : result.coaches.get(i_coach).studentMap.keySet()) {
                            result.students.get(i_st).day_classhours = 0;
                        }

                        if (weekday == 0) {
                            for (int i_st : result.coaches.get(i_coach).studentMap.keySet())
                                result.students.get(i_st).week_classhours = 0;
                        }
                        if (week_class_count == 0) {
                            week_times++;
                            if (week_times >= 3) {
                                break;
                            }
                        } else {
                            week_times = 0;
                        }
                        week_class_count = 0;
                    }
                    day++;
                    class_index = 0;
                } else {
                    //休息日
                    if (week_class_count == 0) {
                        week_times++;
                        if (week_times >= 3) {
                            break;
                        }
                    } else {
                        week_times = 0;
                    }
                    week_class_count = 0;

                    day++;
                    class_index = 0;
                }
            }
        }
        return result;
    }
}
